﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WarehouseWebAPP.code.BLL;

namespace WarehouseWebAPP.Pages.admin.Edit_Entities
{
    public partial class EditVendors : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ErrorLabel.Visible = false;
        }

        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            VendorBLL BLL = new VendorBLL();
            String Previous = BLL.VendorGetByNum((Guid)VendorsDetailsView.SelectedValue)[0].vendor_name;


            if (Previous != args.Value)
            {
                try
                {
                    if (BLL.VendorGetByName(args.Value).Rows.Count > 0)
                        args.IsValid = false;
                    else
                        args.IsValid = true;
                }
                finally
                {
                    //TODO: Dispose
                }

            }
        }

        protected void VendorsGridView_SelectedIndexChanged(object sender, EventArgs e)
        {
            VendorsDetailsView.PageIndex = VendorsGridView.SelectedIndex;
        }

        protected void VendorsDetailsView_ItemDeleting(object sender, DetailsViewDeleteEventArgs e)
        {
            try
            {
                Label Label = (Label)VendorsDetailsView.FindControl("VendorLabel");
                VendorBLL BLL = new VendorBLL();
                Guid guid = BLL.VendorGetByName(Label.Text)[0].vendor_num;

                ComponentBLL CBLL = new ComponentBLL();
                if (CBLL.ComponentGetByVendor(guid).Rows.Count > 0)
                {
                    e.Cancel = true;
                    ErrorLabel.Visible=true;


                }
                LendItemBLL LBLL = new LendItemBLL();
                if (LBLL.lendItemGetBySupplier(guid).Rows.Count > 0)
                {
                    e.Cancel = true;
                    ErrorLabel.Visible = true;

                }

            }
            finally
            {
            }
        }

    }
}
